package com.ewanghuiju.app.ui.watermail.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.BaseActivity;
import com.ewanghuiju.app.base.contract.watermail.WaterSureOrderContract;
import com.ewanghuiju.app.component.ImageLoader;
import com.ewanghuiju.app.component.RxBus;
import com.ewanghuiju.app.model.bean.response.AddressListResponBean;
import com.ewanghuiju.app.model.bean.response.WaterShopResponBean;
import com.ewanghuiju.app.model.event.SendEvent;
import com.ewanghuiju.app.model.http.response.BaseResponse;
import com.ewanghuiju.app.presenter.watermail.WaterSureOrderPresenter;
import com.ewanghuiju.app.ui.my.activity.ChangeBodyWithCodeActivity;
import com.ewanghuiju.app.ui.my.activity.ShopAddressListActivity;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.MD5Util;
import com.ewanghuiju.app.util.StartActivityUtil;
import com.ewanghuiju.app.util.StringUtil;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.ewanghuiju.app.widget.popupwindow.CustomBasePopup;
import com.ewanghuiju.app.widget.popupwindow.PswKeyboardPopup;
import com.ewanghuiju.app.widget.pswkeyboard.OnPasswordInputFinish;
import com.gyf.immersionbar.ImmersionBar;
import com.makeramen.roundedimageview.RoundedImageView;
import com.muddzdev.styleabletoast.StyleableToast;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sj  qq:751943509
 * @class 水滴确认订单
 * @time 2020/2/19
 */
public class WaterSureOrderActivity extends BaseActivity<WaterSureOrderPresenter> implements WaterSureOrderContract.View {
    @BindView(R.id.iv_position)
    ImageView ivPosition;
    @BindView(R.id.tv_order_name)
    TextView tvOrderName;
    @BindView(R.id.tv_order_number)
    TextView tvOrderNumber;
    @BindView(R.id.layout_address)
    LinearLayout layoutAddress;
    @BindView(R.id.rv_shop)
    RoundedImageView rvShop;
    @BindView(R.id.tv_order_title)
    TextView tvOrderTitle;
    @BindView(R.id.tv_order_num)
    TextView tvOrderNum;
    @BindView(R.id.tv_order_yunfei)
    TextView tvOrderYunfei;
    @BindView(R.id.tv_order_paymoney)
    TextView tvOrderPaymoney;
    @BindView(R.id.tv_order_topay)
    TextView tvOrderTopay;
    @BindView(R.id.tv_spe_price)
    TextView tvSpePrice;
    @BindView(R.id.tv_address_text)
    TextView tvAddressText;
    @BindView(R.id.layout_address_phone)
    LinearLayout layoutAddressPhone;
    @BindView(R.id.tv_default)
    TextView tvDefault;
    //订单备注
    @BindView(R.id.et_order_remarks)
    EditText etOrderRemarks;

    private WaterShopResponBean waterShopResponBean;
    private AddressListResponBean addressListResponBean;

    private Animation showAnimation, dismissAnimation;

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_water_sure_order;
    }

    @Override
    protected void initEventAndData() {
        waterShopResponBean = (WaterShopResponBean) getIntent().getSerializableExtra(Constants.WATER_SURE_ORDER_INFO);
        if (waterShopResponBean != null) {
            tvOrderTitle.setText(waterShopResponBean.getGoods_title());
            tvSpePrice.setText("水滴" + waterShopResponBean.getPrice());
            tvOrderPaymoney.setText(waterShopResponBean.getPrice() + "水滴");
            ImageLoader.load(mContext, waterShopResponBean.getGoods_img(), rvShop);
            if (waterShopResponBean.getIs_free() == 0) {
                tvOrderYunfei.setText("免运费");
            } else {
                tvOrderYunfei.setText(waterShopResponBean.getPostage()+"");
            }
        }
        layoutAddressPhone.setVisibility(View.GONE);
        tvAddressText.setText("请选择收货地址");

        etOrderRemarks.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s.toString()) && s.toString().length() >= 30) {
                    StyleableToast.makeText(mContext, "备注不能超过30字", Toast.LENGTH_SHORT, R.style.mytoast).show();
                }
            }
        });
    }

    @Override
    public void showSelectedAddress(AddressListResponBean mAddressListResponBean) {
        if (mAddressListResponBean != null) {
            addressListResponBean = mAddressListResponBean;
            layoutAddressPhone.setVisibility(View.VISIBLE);
            tvDefault.setVisibility(addressListResponBean.getIs_default() == 1 ? View.VISIBLE : View.GONE);
            tvOrderName.setText(mAddressListResponBean.getName());
            tvOrderNumber.setText(mAddressListResponBean.getMobile());
            tvAddressText.setText("地址：" + mAddressListResponBean.getProvince() + mAddressListResponBean.getCity() + mAddressListResponBean.getCountry() + mAddressListResponBean.getDetail());
        }
    }

    @Override
    public void showPay(BaseResponse baseResponse) {
        LoadingDialogUtils.dismissDialog_ios();
        StyleableToast.makeText(mContext, "兑换成功", Toast.LENGTH_SHORT, R.style.mytoast).show();
        //通知更新   刷新用户信息
        RxBus.getDefault().post(new SendEvent("", Constants.TYPE_GET_USERINFO));
        //刷新商品信息
        RxBus.getDefault().post(new SendEvent("", Constants.TYPE_GET_WATER_SHOP_INFO));
        //支付完成
        new StartActivityUtil(mContext, WaterOrderListActivity.class)
                .startActivity(false);
        super.onBackPressedSupport();
        slideRightIn();

    }

    @Override
    public void showPayError(int code, String msg) {
        //实付失败
    }

    @Override
    public void isbindPwd() {
        //绑定成功
        PswKeyboardPopup pswKeyboardPopup = new PswKeyboardPopup(mContext);
        pswKeyboardPopup.setPopupGravity(Gravity.BOTTOM);
        pswKeyboardPopup.setOnFinishInput(new OnPasswordInputFinish() {
            @Override
            public void inputFinish(String password) {
                pswKeyboardPopup.dismiss();
                if (!StringUtil.isInteger(password)) {
                    StyleableToast.makeText(mContext, "密码输入格式错误", Toast.LENGTH_SHORT, R.style.mytoast).show();
                    pswKeyboardPopup.dismiss();
                    return;
                }
                String orderRemarks = etOrderRemarks.getText().toString();
                String md5Pwd = MD5Util.getMD5(MD5Util.getMD5(password));
                LoadingDialogUtils.show(mContext);
                mPresenter.nowPay(waterShopResponBean.getId() + "", "1",
                        addressListResponBean.getId() + "", md5Pwd,TextUtils.isEmpty(orderRemarks) ? "" : orderRemarks);
            }
        });

        //弹框执行动画
        float fromX = 0;
        float fromY = 1f;
        float toX = 0;
        float toY = 0;
        showAnimation = createTranslateAnimation(fromX, toX, fromY, toY);
        dismissAnimation = createTranslateAnimation(toX, fromX, toY, fromY);

        pswKeyboardPopup.setShowAnimation(showAnimation);
        pswKeyboardPopup.setDismissAnimation(dismissAnimation);

        pswKeyboardPopup.showPopupWindow();
    }

    private CustomBasePopup customBasePopup;

    @Override
    public void nobindPwd() {
        //未绑定支付密码
        if (customBasePopup != null) {
            customBasePopup.showPopupWindow();
            return;
        }
        customBasePopup = new CustomBasePopup(mContext, "前往设置支付密码？", "", "",
                new CustomBasePopup.PopupWindowCallback() {
                    @Override
                    public void sureClickCallback() {
                        new StartActivityUtil(mContext, ChangeBodyWithCodeActivity.class).putExtra(Constants.SETTING_TYPE, 2)
                                .startActivity(true);
                    }

                    @Override
                    public void cancelClickCallback() {

                    }
                });
        customBasePopup.setPopupGravity(Gravity.CENTER);
        customBasePopup.showPopupWindow();
    }

    @OnClick({R.id.tv_order_topay, R.id.layout_address_main})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.tv_order_topay:
                //立即支付
                if (waterShopResponBean == null) {
                    StyleableToast.makeText(mContext, "商品信息出错", Toast.LENGTH_SHORT, R.style.mytoast).show();
                    return;
                }
                if (addressListResponBean == null || addressListResponBean.getId() < 0) {
                    StyleableToast.makeText(mContext, "请选择地址", Toast.LENGTH_SHORT, R.style.mytoast).show();
                    return;
                }

                mPresenter.getIsbindPwd();

                break;
            case R.id.layout_address_main:
                new StartActivityUtil(mContext, ShopAddressListActivity.class).putExtra(Constants.TO_ADDRESS_LIST_TYPE, 2)
                        .startActivity(true);
                break;
            default:
                break;
        }
    }

    @Override
    public void onBackPressedSupport() {

        CustomBasePopup customBasePopup = new CustomBasePopup(mContext, "确认要放弃付款吗？", "继续支付", "暂时放弃",
                new CustomBasePopup.PopupWindowCallback() {
                    @Override
                    public void sureClickCallback() {
                    }

                    @Override
                    public void cancelClickCallback() {
                        WaterSureOrderActivity.super.onBackPressedSupport();
                    }
                });
        customBasePopup.setPopupGravity(Gravity.CENTER);
        customBasePopup.showPopupWindow();
    }

    @Override
    protected int getTitleIntText() {
        return R.string.water_sure_title;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this).titleBar(mToolbar)
                .statusBarDarkFont(true)
                .init();
    }
}
